/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFGPrompt
  \brief A transient frame - for prompting a user dialog
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2024-05-11
  \date 2024-05-11 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2024-05-11   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#include <unistd.h>
#include <TGButton.h>
#include <TGLabel.h>
#include <TGLayout.h>
#include <TGResourcePool.h>

#include "ETFGPrompt.h"
#include "ETFMsg.h"

// #define form ETFMsg::Form

  // m is the host frame from which this frame prompts
ETFGPrompt::ETFGPrompt(const TGWindow *p, const TGWindow *m, const string &text,
bool simple, int *useroption, int w, int h, unsigned options)
 : TGTransientFrame(p, m, w, h, options), fWidth(w), fHeight(h), fSimple(simple),
 fText(text), fUserOption(useroption){
  SetWindowName("GANA - User Dialog");
  SetCleanup(kDeepCleanup);
  Initialize();
  Configure();
  gClient->WaitFor(this);
} // end ctor

ETFGPrompt::~ETFGPrompt(){
  DeleteWindow();
} // end dtor

// create all the graphical objects
void ETFGPrompt::Initialize(){
  TGLayoutHints *l = new TGLayoutHints(kLHintsCenterX | kLHintsCenterY
    | kLHintsExpandX | kLHintsExpandY, 2, 2, 2, 2);
  // TGLayoutHints *lx = new TGLayoutHints(kLHintsCenterX | kLHintsCenterY
  //   | kLHintsExpandX, 2, 2, 2, 2);
  const int w = GetWidth(), h = GetHeight();

  // the text to be displayed //
  fPad = new TGCompositeFrame(this, w, h-40, kVerticalFrame | kSunkenFrame);
  AddFrame(fPad, l);
  fTLabel = new TGLabel(fPad, fText.data());
  fTLabel->SetTextFont("-adobe-courier-bold-r-*-*-15-*-*-*-*-*-iso8859-1");
  fPad->AddFrame(fTLabel, l);

  // the buttons //
  fHBtn = new TGCompositeFrame(this, w, 40, kHorizontalFrame | kFixedHeight);
  AddFrame(fHBtn, new TGLayoutHints(kLHintsCenterX | kLHintsExpandX |
    kLHintsLeft, 2, 2, 2, 2));
  if(fSimple) fConfirm = new TGTextButton(fHBtn, "&Close");
  else fConfirm = new TGTextButton(fHBtn, "&Yes");
  fConfirm->Connect("Clicked()", "ETFGPrompt", this, "Confirm()");
  fHBtn->AddFrame(fConfirm, l);
  if(fSimple) return; // no cancel button
  fCancel = new TGTextButton(fHBtn, "&No");
  fCancel->Connect("Clicked()", "ETFGPrompt", this, "Cancel()");
  fHBtn->AddFrame(fCancel, l);
} // end member function Initialize

void ETFGPrompt::SetText(const string &t){
  fTLabel->SetText(t.data());
  Resize();
} // end member function SetText

// visualize all the graphical objects
void ETFGPrompt::Configure(){
  MapSubwindows();
  Resize(GetDefaultSize()); // arrange them in proper position
  Resize();
  // Resize(fWidth, fHeight); // arrange them in proper position
  CenterOnParent();
  MapWindow();
} // end member function configure

void ETFGPrompt::Confirm(){
  if(fUserOption) *fUserOption = 1;
  // if(!fSimple) SetText(form("User Selection: Confirm"));
  CloseWindow();
} // end member function Confirm

void ETFGPrompt::Cancel(){
  if(fUserOption) *fUserOption = -1;
  // SetText(form("User Selection: Cancel"));
  CloseWindow();
} // end member function Cancel
